import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

#测试博客登录页面

class BlogLogin:
    url=''
    driver=''
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #成功登录测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 添加屏幕截图
        time.sleep(1)
        BlogDriver.getScreenShot()
        #能够找到博客首页的用户昵称，说明登录成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
    #异常登录测试用例
    def LoginFailTest(self):
        self.driver.implicitly_wait(3)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        #密码错误
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #检查是否登录失败
        WebDriverWait(self.driver, 10).until(EC.alert_is_present())  # 等待弹窗
        alert = self.driver.switch_to.alert  # 切换到弹窗
        assert alert.text == "密码错误"
        alert.accept()


